<?php
/**
 * Created by PhpStorm.
 * User: jc234316
 * Date: 5/15/14
 * Time: 4:05 PM
 */
require 'init.php';
define("IMG_HEIGHT", 130);

if (isset($_POST['submit'])) {

    if ($_REQUEST['submit'] == "X")
    {
        $_SESSION['totalItems'] -= $_SESSION['cart'][$_REQUEST['row']]['quantity'];
        unset($_SESSION['cart'][$_REQUEST['row']]);
        $_SESSION['cart'] = array_values($_SESSION['cart']);
        if (count($_SESSION['cart']) == 0)
        {
            $_SESSION['totalItems'] = 0;
            unset($_SESSION['cart']);
        }
        header('Location: ' . $_SERVER['PHP_SELF']);
    }
    else if ($_REQUEST['submit'] == "Update")
    {
        if (empty($_POST['quantity']) === true || ctype_digit($_POST['quantity']) == false)
        {
            $message[] = 'Quantity can not be empty and can only contain a whole number';
        }
        else
        {
            if ($_SESSION['cart'][$_REQUEST['row']]['quantity'] < $_REQUEST['quantity']) {
                $_SESSION['totalItems'] += ($_REQUEST['quantity'] - $_SESSION['cart'][$_REQUEST['row']]['quantity']);
                $_SESSION['cart'][$_REQUEST['row']]['quantity'] = $_REQUEST['quantity'];
            } else if ($_SESSION['cart'][$_REQUEST['row']]['quantity'] > $_REQUEST['quantity']) {
                $_SESSION['totalItems'] -= ($_SESSION['cart'][$_REQUEST['row']]['quantity'] - $_REQUEST['quantity']);
                $_SESSION['cart'][$_REQUEST['row']]['quantity'] = $_REQUEST['quantity'];
            }
        }
    }
    else if ($_REQUEST['submit'] == "Checkout")
    {
        header('Location: billing.php');
        exit();
    }
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Shopping Cart</title>
    <meta name="description" content="Shopping cart full of awesome steampunk clothing">
    <meta name="keywords" content="steampunk, tshirts, shopping">
</head>

<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png" alt="Left Steampunk Cog"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png" alt="Right Steampunk Cog"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="contentFrame">
            <div id="cartContentsContainer">
                <h3>Items in your shopping cart:</h3>
                <?php
                if (!isset($_SESSION['cart'])) {
                    echo "<p>" . "You currently have no items in your cart" . "</p></br>";
                    echo "<a href='gallery.php'>View Gallery</a></br>";
                    echo "<a href='index.php'>Return to Home Page</a>";
                    exit();
                } else {
                    $linePrice = 0.0;
                    $totalPrice = 0.0;
                    $row = 0;

                    foreach ($_SESSION['cart'] as $cartItem) {
                        $row++;
                        $sql = "SELECT Path FROM Images WHERE ProductID = ?";
                        $query = $db->prepare($sql);
                        $query->bindValue(1, $cartItem['productID']);

                        try {
                            $query->execute();
                            if ($query) {
                $record = $query->fetch();
                ?>
                <form id="cartContents" name="cartContents" method="post" action="">
                    <table id="cart">
                        <thead <?php if ($row !== 1) echo "style='display: none'" ?>>
                        <tr>
                            <th class="cartThumbnail"></th>
                            <th class="cartNameCol">Name</th>
                            <th class="cartSizeCol">Size</th>
                            <th class="cartPriceCol">Price</th>
                            <th class="cartQuantityCol">Quantity</th>
                            <th class="cartTotalCol">Total</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td class="cartThumbnail">
                                <?php
                                // The following code is from a code example in CP2010 - Lecture 16-2 Shopping Cart Demo.
                                if (isset($record['Path'])) {
                                    $size = getimagesize($record['Path']);
                                    $aspectRatio = $size[0] / $size[1];
                                    $width = IMG_HEIGHT * $aspectRatio;
                                ?>
                                <img src="<?php echo $record['Path']; ?>" width="<?php echo $width; ?>"
                                     height="<?php echo IMG_HEIGHT; ?>"
                                     alt="<?php echo $cartItem['name']; ?>"/>
                                <?php
                                }
                                ?>
                            </td>
                            <td class="cartNameCol"><?php echo $cartItem['name']; ?></td>
                            <td class="cartSizeCol"><?php echo $cartItem['size']; ?></td>
                            <td class="cartPriceCol"><?php echo "$" . number_format(($cartItem['price']), 2, ".", ""); ?></td>
                            <td class="cartQuantityCol"><input type='text' size='5' name='quantity'
                                                               id='quantity'
                                                               value='<?php echo $cartItem['quantity']; ?>'</td>
                            <td class="cartTotalCol"> <?php $linePrice = (round((float)$cartItem['price'], 2) * (float)$cartItem['quantity']);
                                echo "$" . number_format(($linePrice), 2, ".", ""); ?>
                            <td class="cartUpdateBtn"><input type="submit" name="submit" value="Update"/></td>
                            <td class="cartDeleteBtn"><input type="submit" name="submit" value="X" class="red"/>
                            </td>
                        </tr>
                        <?php
                        }
                        } catch (PDOException $e) {
                            echo $e->getMessage();
                        }
                        $totalPrice += $linePrice;
                        }
                        ?>
                        <tr>
                            <td colspan="8">
                                <div
                                    id="cartTotal"><?php echo 'Total Price: $' . number_format(($totalPrice), 2, ".", ""); ?>
                                    <br/>
                                    <hr/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"><a href='gallery.php'>Continue Shopping</a></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td colspan="2" class="cartCheckOutBtn"><input type="submit" name="submit"
                                                                           value="Checkout"/></td>
                        </tr>
                        <?php
                        }
                ?>
                        </tbody>
                    </table>
                    <input type='hidden' name='row' id='row' value='<?php echo($row - 1); ?>'/>
                    <div id="statusBar">
                        <?php
                        if (empty($message) === false) {
                            echo "<script type='text/javascript'>document.getElementById('statusBar').style.display = 'block';</script>";
                            echo '<p>' . implode('<p></p>', $message) . '</p>';
                        }
                        ?>
                    </div>
                </form>
            </div>
        </div>
        <div id="push"></div>
    </div>
    <?php require 'footer.php' ?>
</div>
</body>
</html>